package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.traffic.RoadTrafficQuery;
import com.amap.api.services.traffic.TrafficSearch;
import com.amap.api.services.traffic.TrafficStatusEvaluation;
import com.amap.api.services.traffic.TrafficStatusInfo;
import com.amap.api.services.traffic.TrafficStatusResult;
import com.makeunion.gdlbsdemo.R;

import java.util.List;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class TrafficInfoActivity extends AppCompatActivity implements
        TrafficSearch.OnTrafficSearchListener {

    /*
        道路等级
        1：高速（例如：京藏高速）；
        2：城市快速路、国道(例如：三环路、103国道)；
        3：高速辅路（例如：G6辅路）；
        4：主要道路（例如：长安街、三环辅路路）；
        5：一般道路（例如：彩和坊路）；
        6：无名道路；
        注：以上道路数值设置之后存在从大到小的包含关系，如道路等级5代表一般道路，设置后会包含高速、城市快速路、高速辅路、主要道路以及一般道路的交通态势查询。
    */

    TextView descriptionTxt;

    TextView evaluationTxt;

    TextView roadsTxt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traffic_info);

        descriptionTxt = (TextView)findViewById(R.id.description_txt);
        evaluationTxt = (TextView)findViewById(R.id.evaluation_txt);
        roadsTxt = (TextView)findViewById(R.id.roads_txt);

        TrafficSearch trafficSearch = new TrafficSearch(this);
        trafficSearch.setTrafficSearchListener(this);
        RoadTrafficQuery roadTrafficQuery = new RoadTrafficQuery(
                "将台路", //道路名称
                "110000", //城市编号
                TrafficSearch.ROAD_LEVEL_NORMAL_WAY //道路级别
        );
        trafficSearch.loadTrafficByRoadAsyn(roadTrafficQuery);
    }

    @Override
    public void onRoadTrafficSearched(TrafficStatusResult result, int i) {
        String description = result.getDescription(); //返回路况综合情况描述
        Log.e("TAG", "description=" + description);
        descriptionTxt.setText(description);

        // evaluation
        TrafficStatusEvaluation evaluation = result.getEvaluation(); //包含对路况的评价，拥堵占比等
        String description1 = evaluation.getDescription();
        String blocked = evaluation.getBlocked();
        String congested = evaluation.getCongested();
        String expedite = evaluation.getExpedite();
        String status = evaluation.getStatus();
        String unknown = evaluation.getUnknown();
        String evaluationLog = "描述=" + description1 + ", 堵车=" + blocked + ", 拥堵=" + congested + ", 加快=" + expedite + ", 状态=" + status + ", 未知=" + unknown;
        Log.e("TAG", evaluationLog);
        evaluationTxt.setText(evaluationLog);

        List<TrafficStatusInfo> roads = result.getRoads(); //道路列表，包含道路坐标点集以及各道路路况信息
        if (roads == null) {
            roadsTxt.setText("null");
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (TrafficStatusInfo road : roads) {
            String status1 = road.getStatus();
            int angle = road.getAngle();
            List<LatLonPoint> coordinates = road.getCoordinates();
            String direction = road.getDirection();
            String lcodes = road.getLcodes();
            String name = road.getName();
            float speed = road.getSpeed();
            String roadLog = "名称=" + name + ", lcodes=" + lcodes + ", 方向=" + direction + ", 速度=" + speed + ", angle=" + angle + ", 状态=" + status1;
            Log.e("TAG", roadLog);
            sb.append(roadLog).append("\n").append("\n");
        }
        roadsTxt.setText(sb.toString());
    }
}
